package com.zhss.designpattern.chain;

/**
 * @author fanwei
 * @desc
 */
public class ChainPatternDemo {

    public static  abstract class Handler{

        protected Handler successor;

        public Handler(Handler successor) {
            this.successor = successor;
        }
        public abstract void execute();
    }

    static class HandlerA extends Handler{

        public HandlerA(Handler successor) {
            super(successor);
        }

        @Override
        public void execute() {
            System.out.println("执行功能A");
            if(successor != null) {
                successor.execute();
            }
        }
    }

    static class HandlerB extends Handler{

        public HandlerB(Handler successor) {
            super(successor);
        }

        @Override
        public void execute() {
            System.out.println("执行功能B");
            if(successor != null) {
                successor.execute();
            }
        }
    }

    static class HandlerC extends Handler{

        public HandlerC(Handler successor) {
            super(successor);
        }

        @Override
        public void execute() {
            System.out.println("执行功能C");
            if(successor != null) {
                successor.execute();
            }
        }
    }

    public static void main(String[] args) {
        // 业务流程1
        Handler thirdHandler = new HandlerC(null);
        Handler secondHandler = new HandlerB(thirdHandler);
        Handler firstHandler = new HandlerA(secondHandler);
        firstHandler.execute();

        // 业务流程2
        thirdHandler = new HandlerC(null);
        secondHandler = new HandlerA(thirdHandler);
        firstHandler = new HandlerB(secondHandler);
        firstHandler.execute();
    }


}
